package com.ydgk.service.impl;

import com.ydgk.bean.*;
import com.ydgk.dao.*;
import com.ydgk.exception.StockException;
import com.ydgk.service.OrderService;
import com.ydgk.service.UserService;
import com.ydgk.tool.Tool;
import com.ydgk.vo.AddressInfo;
import com.ydgk.vo.LogisticsInfo;
import com.ydgk.vo.OrderInfo;
import com.ydgk.vo.Pre_orderInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private Pre_orderMapper pre_orderMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private FurnitureKindsMapper furnitureKindsMapper;

    @Autowired
    private FurnitureMapper furnitureMapper;

    @Autowired
    private LogisticsMapper logisticsMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private ExpressMapper expressMapper;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private UserService userService;


    public void insertPre_order(Pre_order pre_order) {
        pre_orderMapper.insert(pre_order);
    }

    public int getPreCount(Integer uid) {
        return pre_orderMapper.getPreCount(uid);
    }

    public List<Pre_order> getPre(Integer uid) {
        List<Pre_order> pre_orderList =  pre_orderMapper.getPre(uid);
        for (Pre_order pre_order: pre_orderList) {
            List<Pre_order> pre_orderList1 = pre_orderMapper.getPreByFkid(pre_order.getFk_fkid());
            if(pre_orderList1.size()>1){
                int count = 0;
                Pre_order newPre = null;
                for (Pre_order pre: pre_orderList1) {
                    count += pre.getCount();
                    newPre = pre;
                    pre_orderMapper.deleteById(pre.getPoid());
                }
                newPre.setCount(count);
                pre_orderMapper.insert(newPre);
            }
        }
        return pre_orderMapper.getPre(uid);
    }

    public void deletePre_order(Integer poid) {
        pre_orderMapper.deleteById(poid);
    }

    public void updatePre_order(Pre_order pre_order) {
        pre_orderMapper.updateById(pre_order);
    }

    public Pre_order findPreById(Integer poid) {
        return pre_orderMapper.findPreById(poid);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    public void insertOrder(Integer[] poids, Order order, String eWay) {
        order.setOnum(Tool.getOrderNumber());
        order.setOtype(0);
        order.setOtime(new Timestamp(System.currentTimeMillis()));
        orderMapper.insert(order);
        int count = 0;
        int stock = 0;
        for (int i = 0; i < poids.length; i++) {
           Pre_order pre_order = pre_orderMapper.findPreById(poids[i]);
           pre_order.setO_oid(order.getOid());
           pre_orderMapper.updateById(pre_order);
           count = pre_order.getCount();
           FurnitureKinds furnitureKinds = furnitureKindsMapper.findById(pre_order.getFk_fkid());
           stock = furnitureKinds.getStock();
           if(count <= stock){
               furnitureKinds.setStock(stock-count);
               furnitureKinds.setSales(furnitureKinds.getSales()+pre_order.getCount());
               furnitureKindsMapper.updateById(furnitureKinds);
               Furniture furniture = furnitureMapper.findById(furnitureKinds.getF_fid());
               furniture.setSales(furniture.getSales()+pre_order.getCount());
               furnitureMapper.updateById(furniture);
           }else{
               throw new StockException("库存不足");
           }
        }
        if(eWay.equals("e1")){
            logisticsMapper.insert(new Logistics(null,null,null,order.getOid(),1));
        }else if(eWay.equals("e2")){
            logisticsMapper.insert(new Logistics(null,null,null,order.getOid(),2));
        }else if(eWay.equals("e3")){
            logisticsMapper.insert(new Logistics(null,null,null,order.getOid(),3));
        }else if(eWay.equals("e4")){
            logisticsMapper.insert(new Logistics(null,null,null,order.getOid(),4));
        }
    }

    public int findAllOrderCount(Map<String, Object> factorMap) {
        return orderMapper.findAllCount(factorMap);
    }

    public List<Order> findAllOrder(Map<String, Object> factorMap) {
        return orderMapper.findAll(factorMap);
    }


    public OrderInfo getOrderInfo(Order order) {
        List<Pre_order> pre_orderList = pre_orderMapper.findByOid(order.getOid());
        List<Pre_orderInfo> pre_orderInfoList = new ArrayList<Pre_orderInfo>();
        for (Pre_order pre : pre_orderList) {
            FurnitureKinds furnitureKinds = furnitureKindsMapper.findById(pre.getFk_fkid());
            Furniture furniture = furnitureMapper.findById(furnitureKinds.getF_fid());
            pre_orderInfoList.add(new Pre_orderInfo(pre,furniture,furnitureKinds));
        }
        Address address = addressMapper.findById(order.getAd_adid());
        AddressInfo addressInfo = userService.getAddressInfo(address);
        LogisticsInfo logisticsInfo = null;
        if(order.getOtype() == 1){
            Logistics logistics = logisticsMapper.findByOid(order.getOid());
            Express express = expressMapper.findById(logistics.getE_eid());
            List<Message> messageList = messageMapper.findByLid(logistics.getLid());
            logisticsInfo = new LogisticsInfo(logistics,messageList,express);
        }
        return new OrderInfo(order,pre_orderInfoList,addressInfo,logisticsInfo);
    }

    public Order findOrderById(Integer oid) {
        return orderMapper.findById(oid);
    }

    public void updateOrder(Order order) {
        orderMapper.updateById(order);
    }

    public int findOrderCountByOtype(Integer otype,Integer oid) {
        return orderMapper.findCountByOtype(otype,oid);
    }


}
